package com.guyaogg.gulimall.cart.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 顾遥
 */
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {

    /**
     * 注入自定义线程池
     * @param pool
     * @return
     */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
        int coreSize = pool.getCoreSize() != null ? pool.getCoreSize() : 20;
        int maxSize = pool.getMaxSize() != null ? pool.getMaxSize() : 200;
        int keepAliveTime = pool.getKeepAliveTime() != null ? pool.getKeepAliveTime() : 60;
        int blockingQueueSize = pool.getBlockingQueueSize() != null ? pool.getBlockingQueueSize() : 100000;

        return new ThreadPoolExecutor(coreSize, maxSize,
                keepAliveTime, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(blockingQueueSize),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
